package com.lilinling.yinzhi.user.controller;

import com.lilinling.common.exception.BizCodeEnum;
import com.lilinling.common.utils.JwtInfo;
import com.lilinling.common.utils.JwtUtils;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.R;
import com.lilinling.yinzhi.user.entity.UserEntity;
import com.lilinling.yinzhi.user.exception.AccountExsitException;
import com.lilinling.yinzhi.user.exception.MobileExsitException;
import com.lilinling.yinzhi.user.exception.NicknameExsitException;
import com.lilinling.yinzhi.user.service.UserService;
import com.lilinling.yinzhi.user.vo.UserLoginVo;
import com.lilinling.yinzhi.user.vo.UserRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;


/**
 * 用户
 *
 * @author lichenglin
 * @email lilinling15@gmail.com
 * @date 2021-01-19 22:19:29
 */
@Api("用户接口")
@RestController
@RequestMapping("user/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R register(@Valid @RequestBody UserRegisterVo userRegisterVo){

//        if (result.hasErrors()){
//            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
//            redirectAttributes.addAttribute("errors",errors);
//            throw new RRException(BizCodeEnum.VAILD_EXCEPTION.getMsg(),BizCodeEnum.VAILD_EXCEPTION.getCode());
//        }
        try {
            userService.register(userRegisterVo);
        }catch (NicknameExsitException e){
            return R.error(BizCodeEnum.NICKNAME_EXSIT_EXCEPTION.getCode(),BizCodeEnum.NICKNAME_EXSIT_EXCEPTION.getMsg());
        }catch (AccountExsitException e){
            return R.error(BizCodeEnum.ACCOUNT_EXSIT_EXCEPTION.getCode(),BizCodeEnum.ACCOUNT_EXSIT_EXCEPTION.getMsg());
        }catch (MobileExsitException e){
            return R.error(BizCodeEnum.MOBILE_EXSIT_EXCEPTION.getCode(),BizCodeEnum.MOBILE_EXSIT_EXCEPTION.getMsg());
        }
        return  R.ok("注册成功");
    }

    /**
     * 登录
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R login(@Valid @RequestBody UserLoginVo vo){
        String token = userService.login(vo);
        return  R.ok("登录成功").put("token",token);
    }

    /**
     * 获取登录信息
     */
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/get-login-info")
    public R getLoginInfo(HttpServletRequest request){
        try {
            JwtInfo jwtInfo = JwtUtils.getUserIdByJwtToken(request);
            return R.ok().put("userInfo",jwtInfo);
        }catch (Exception e){
            log.error("解析用户信息失败，" + e.getMessage());
            return R.error(BizCodeEnum.FETCH_USERINFO_EXCEPTION.getCode(),BizCodeEnum.FETCH_USERINFO_EXCEPTION.getMsg());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:user:save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
